package com.dyw.ycgovfinance.mvp.open.newslist;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.dyw.ycgovfinance.R;

import com.dyw.ycgovfinance.adapter.list.ListAdapterForOpenNews;
import com.dyw.ycgovfinance.app.CustomApplication;
import com.dyw.ycgovfinance.bean.NewsKindBean;
import com.dyw.ycgovfinance.bean.NewsListBean;
import com.dyw.ycgovfinance.commom.Config;
import com.dyw.ycgovfinance.commom.IntentKey;
import com.dyw.ycgovfinance.mvp.base.BaseActivity;
import com.dyw.ycgovfinance.mvp.base.BaseFragment;
import com.dyw.ycgovfinance.mvp.news.newsdetail.ActivityNewsDetail;




import com.dyw.ycgovfinance.util.ToastUtil;
import com.dyw.ycgovfinance.util.schedulers.SchedulerProvider;
import com.dyw.ycgovfinance.util.viewutil.PicassoBannerLoader;
import com.dyw.ycgovfinance.widget.ListViewForScrollView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by clboys on 2017/6/29.
 */

public class ActivityOpenNewsList extends BaseActivity implements OpenNewsListContract.View, PullToRefreshBase.OnRefreshListener2 {



    OpenNewsListContract.Presenter mPresenter;

    private PullToRefreshListView lvOpenNews;
    private ListView originalLv;
    private RelativeLayout rlEmpty;


    Integer mPageIndex;
    boolean isRefresh;
    boolean canLoad;


    List<NewsListBean> mNewsList = new ArrayList<>();
    ListAdapterForOpenNews mAdapter;

    NewsKindBean mNewsKind;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_news_list);
        initData();
        initTitle(null, null, mNewsKind.getName(), true, null);
        assignView();
        initView();

        mPresenter = new OpenNewsListPresenter(this,SchedulerProvider.getInstance(),CustomApplication.getInstance().getHttpService());
        getOriginalList();
    }

    @Override
    public void initData() {
        mNewsKind = (NewsKindBean) getIntent().getSerializableExtra(IntentKey.INTENT_KEY_NEWS_KIND);
    }

    @Override
    public void assignView() {
        lvOpenNews = (PullToRefreshListView) findViewById(R.id.lv_open_news);
        originalLv = lvOpenNews.getRefreshableView();
        rlEmpty = (RelativeLayout) findViewById(R.id.rl_empty);

    }

    @Override
    public void initView() {


        rlEmpty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getOriginalList();
            }
        });
        lvOpenNews.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        lvOpenNews.setOnRefreshListener(this);
        mAdapter = new ListAdapterForOpenNews(mNewsList, ActivityOpenNewsList.this);
        originalLv.setAdapter(mAdapter);


        originalLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent toNewsDetail = new Intent(ActivityOpenNewsList.this, ActivityNewsDetail.class);
                toNewsDetail.putExtra(IntentKey.INTENT_KEY_NEWS_ID, String.valueOf(mNewsList.get(position - 1).getId()));
                startActivity(toNewsDetail);
            }
        });
    }


    private void getOriginalList() {
        isRefresh = true;
        mPageIndex = 1;
        getNewsList();
    }

    private void getMoreList() {
        if (canLoad) {
            mPageIndex++;
            getNewsList();
        }
    }

    @Override
    public void getNewsList() {
        mPresenter.getNewsList(mPageIndex.toString(), Config.DEFAULT_PAGE_SIZE.toString(), mNewsKind.getTtype(), mNewsKind.getTclass());

    }

    @Override
    public void onGetNewsListSuccess(List<NewsListBean> newsList) {
        lvOpenNews.onRefreshComplete();

        if (isRefresh) {
            mNewsList.clear();
            isRefresh = false;
        }

        if (newsList.size() > 0) {

            if (newsList.size() >= Config.DEFAULT_PAGE_SIZE) {
                lvOpenNews.setMode(PullToRefreshBase.Mode.BOTH);
                canLoad = true;
            } else {
                canLoad = false;
                lvOpenNews.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            }
            mNewsList.addAll(newsList);

        } else {
            canLoad = false;
            lvOpenNews.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

            if (mNewsList.size() == 0) {

                lvOpenNews.setEmptyView(rlEmpty);

            } else {
                ToastUtil.show(ActivityOpenNewsList.this, "已经获取全部数据", 2000);
            }
        }

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetNewsListError(String tip) {
        lvOpenNews.onRefreshComplete();
        ToastUtil.showShort(ActivityOpenNewsList.this, tip);
        mNewsList.clear();
        lvOpenNews.setEmptyView(rlEmpty);

        isRefresh = false;
        canLoad = false;
        mAdapter.notifyDataSetChanged();


    }


    @Override
    public void setPresenter(OpenNewsListContract.Presenter presenter) {

    }

    @Override
    public void showLoading() {
        showProgressDialog(false, null);
    }

    @Override
    public void dismissLoading() {
        dismissProgressDialog();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase pullToRefreshBase) {
        getOriginalList();

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase pullToRefreshBase) {
        getMoreList();
    }
}

